package com.servlet;

import com.bean.Owner;
import com.google.gson.Gson;
import com.service.impl.OwnerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/OwnerServlet/*")
public class OwnerServlet extends HttpServlet {

    OwnerServiceImpl ownerService = new OwnerServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html,charset=utf-8");//命令浏览器用什么编码打开文件

        String pathInfo = request.getPathInfo().substring(1);
        if("listAll".equals(pathInfo)){
            listAll(request,response);
        }else if("listById".equals(pathInfo)){
            listById(request,response);
        }else if("deleteById".equals(pathInfo)){
            deleteById(request,response);
        }else if("removeAll".equals(pathInfo)){
            removeAll(request,response);
        }else if("listAllByPagination".equals(pathInfo)){
            listAllByPagination(request,response);
        }else if("ownerAdd".equals(pathInfo)){
           ownerAdd(request,response);
        }else if("updateById".equals(pathInfo)){
            updateById(request,response);
        }else if("listAllByPaginationSearch".equals(pathInfo)){
            listAllByPaginationSearch(request,response);
        }
    }

    private void listById(HttpServletRequest request, HttpServletResponse response) {
        String name  = request.getParameter("name");
        try {
            List<Owner> oList = ownerService.selectById(name);
            Gson gson = new Gson();
            String s =gson.toJson(oList);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void listAllByPaginationSearch(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));
        String search = request.getParameter("search");

        try {
            List<Owner> ownerList = ownerService.selectAllPaginationSearch(offset,limit,search);
            int total = ownerService.countSearch(search);
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",ownerList);

            Gson gson = new Gson();
            String s =gson.toJson(map);

            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   private void updateById(HttpServletRequest request, HttpServletResponse response) {
        Integer Id = Integer.valueOf(request.getParameter("Id"));
        String ownerId = request.getParameter("ownerId");
        String ownerName = request.getParameter("ownerName");
        String ownerPassword = request.getParameter("ownerPassword");
        String ownerSex = request.getParameter("ownerSex");
        Integer ownerPhone = Integer.valueOf(request.getParameter("ownerPhone"));
        String ownerIdentity = request.getParameter("ownerIdentity");
        String ownerPic = request.getParameter("ownerPic");
        String haveTime = request.getParameter("haveTime");
        Integer buildId = Integer.valueOf(request.getParameter("buildId"));
        Integer unitId = Integer.valueOf(request.getParameter("unitId"));
        String roomNumber = request.getParameter("roomNumber");
        Owner owner = new Owner(Id,ownerId,ownerName,ownerPassword,ownerSex,ownerPhone,ownerIdentity,ownerPic,haveTime,buildId,unitId,roomNumber);
        try {
            boolean b = ownerService.update(owner);
            if(b){
                response.getWriter().print("success");
            }else{
                response.getWriter().print("");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void ownerAdd(HttpServletRequest request, HttpServletResponse response) {

        String ownerId = request.getParameter("ownerId");
        String ownerName = request.getParameter("ownerName");
        String ownerPassword = request.getParameter("ownerPassword");
        String ownerSex = request.getParameter("ownerSex");
        Integer ownerPhone = Integer.valueOf(request.getParameter("ownerPhone"));
        String ownerIdentity = request.getParameter("ownerIdentity");
        String ownerPic = request.getParameter("ownerPic");
        String haveTime = request.getParameter("haveTime");
        Integer buildId = Integer.valueOf(request.getParameter("buildId"));
        Integer unitId = Integer.valueOf(request.getParameter("unitId"));
        String roomNumber = request.getParameter("roomNumber");
        Owner owner = new Owner(null,ownerId,ownerName,ownerPassword,ownerSex,ownerPhone,ownerIdentity,ownerPic,haveTime,buildId,unitId,roomNumber);

        try {
            boolean b = ownerService.insert(owner);
            if(b){
                request.setAttribute("msg","新增成功");
            }else{
                request.setAttribute("msg","新增失败");
            }
            request.getRequestDispatcher("/property/owner_add.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAllByPagination(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));

        try {
            List<Owner> ownerList = ownerService.selectAllPagination(offset,limit);
            int total = ownerService.count();
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",ownerList);

            Gson gson = new Gson();
            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void removeAll(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");

        String[] idsArr = ids.split(",");
        try {
            boolean b = ownerService.removeAll(idsArr);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) {
        Integer Id = Integer.valueOf(request.getParameter("Id"));
        try {
            boolean b = ownerService.deleteById(Id);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAll(HttpServletRequest request, HttpServletResponse response) {

        try {
            List<Owner> ownerList = ownerService.selectAll();

            Gson gson = new Gson();
            String s = gson.toJson(ownerList);
            System.out.println(s);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
